package kuangstudy;

/**
 * @author:李雄
 * @create:2022-07-08 16:43
 */
public class ThreadBank {
    public static void main(String[] args) {
        Count I = new Count(100);
        MyBank salary1 = new MyBank(I);
        MyBank salary2 = new MyBank(I);
        Thread thread = new Thread(salary1, "我");
        Thread thread1 = new Thread(salary2, "他");
        thread.start();
        thread1.start();
    }
}

class MyBank implements Runnable {
    Count count;
    boolean flag = true;

    public MyBank(Count count) {
        this.count = count;
    }

    @Override
    public void run() {
        while (flag) {
            if (count.money > 0) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                getMoney();
            } else {
                flag = false;
                return;
            }
        }
    }

    //取款
    public void getMoney() {
        System.out.println(Thread.currentThread().getName() + "取了" + count.money-- + "元");
    }
}

class Count {
    int money;

    public Count(int money) {
        this.money = money;
    }

}